using Systemathics.FrameWork;

namespace Systemathics.Indicators
{
    using System.Collections.Generic;
    using System.Linq;

    public sealed class TDAntiDiff : Indicator
    {
        private readonly Queue<Bar> PastBar;

        public TDAntiDiff(Instrument instrument, Data dt, BarType bt, int value) : base(instrument,dt,  bt, value)
        {
            PastBar = new Queue<Bar>();
        }
        protected override void Calculate(object price)
        {
            var Bar = price as Bar;

            if (PastBar.Count < 2)
                PastBar.Enqueue(Bar.Clone() as Bar);
            else
            {
                PastBar.Enqueue(Bar.Clone() as Bar);

                if ((PastBar.First().Close < PastBar.ElementAt(1).Close) && (PastBar.ElementAt(1).Close < PastBar.ElementAt(1).Open) && (Bar.Open < Bar.Close))
                {
                    Add(Time.Now, -1);
                    IndicatorCalculated(this);
                }
                else if ((PastBar.First().Close > PastBar.ElementAt(1).Close) && (PastBar.ElementAt(1).Close > PastBar.ElementAt(1).Open) && (Bar.Open > Bar.Close))
                {
                    Add(Time.Now, +1);
                    IndicatorCalculated(this);
                }

                PastBar.Dequeue();
            }
        }
    }
}